const API = require('../../utils/api');
Page({
  data: {
    currentnoticePageid: 1,
    totalnoticesNum: 5,
    notices: [],
    tasks: {
      isSigned: false,
      allTasks: [
        {
          bookId: '123123121231231',
          img: 'https://file.lantingshucheng.com/myapp/dazhuzai.jpg',
          bookName: '大主宰',
          bookDes: '大千世界,位面交汇,万族林立,群雄荟萃,一位位来自下位面的天之至尊,在这无尽世界,演绎着令人向往的传奇,追求着那主宰之',
          bookHot: 4,
          progressRate: 5
        },
        {
          bookId: '1231231231',
          img: 'https://file.lantingshucheng.com/myapp/chenyuan.jpg',
          bookName: '尘缘',
          bookDes: '那一天，我摇动所有的经桶，不为超度，只为触摸你的指尖；那一年，在山路匍匐，不为觐见',
          bookHot: 2,
          progressRate: 2
        }
      ]
    },
    recBooks:[],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    currentnotice: 1,
    avatar: '',
    hasReadTime: 0,
    history: 0,
    hasBookNum: 0
  },
  onLoad: function () {
    var self = this;
    // 得到通知的数据
    self.getnotices();
    // 从缓存中读取用户信息
    let a = wx.getStorageSync("userInfo")
    self.setData({
      avatar:a.avatarUrl
    })
  },
  onShow:function(){
    var self = this;
    this.handleGetBookShelf()
    this.handleGetReadTime()
    API.getrecbook().then(resp=>{
      self.setData({
        recBooks:resp
      })
    })
    var id = wx.getStorageSync('id');
    API.gettotleuserreadhistory({
      id:id
    }).then(resp=>{
      self.setData({
        history:resp.length
      })
    })
  },
  getnotices: function (e) {
    API.getnewnotice().then(resp=>{
      console.log(resp)
            this.setData({totalnoticesNum: resp.length,notices: resp});
    })
  },
  changenotice: function(e){
    var currentIndex = e.detail.current;
    console.log(currentIndex)
    this.setData({currentnoticePageid: currentIndex});
  },
  handleGetBookShelf:function(){
    var id = wx.getStorageSync('id');
    API.getbookshelf({
      "id":id
    }).then(resp=>{
      this.setData({
        hasBookNum:resp.length
      })
    })
  },
  handleGetReadTime:function(){
    var id = wx.getStorageSync('id');
    API.getuser({
      id:id
    }).then(resp=>{
      this.setData({
        hasReadTime:resp.readTime
      })
    })
  },
  getDetail:function(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../details/index?id=' + id,
    })
  }
})
